//
//  AppDelegate.m
//  UI01_UIWindow_UIView_UILabel
//
//  Created by Scott on 16/3/9.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "MyView.h"
#import "Mybutton.h"

#define WIDTH self.window.frame.size.width / 5
#define HEIGHT self.window.frame.size.height / 8


@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc {
    
    [_window release];
    [super dealloc];
    
}

#pragma mark - 知识点1: UIWindow
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 创建window对象.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];
    
    // 指定window的根视图控制器
    ViewController *vc = [[ViewController alloc] init];
    
    self.window.rootViewController = vc;
    
    // 指定window为主Window并可见
    [self.window makeKeyAndVisible];
    
    
    // 知识点2: UIView
    [self createView];
    
    // 知识点3: View图层管理
    [self managingtheViewHierarchy];
    
    // 知识点4: UIButton
    [self createButton];
    
    // 内存管理
    [vc release];
    [_window release];
    
    return YES;
}


#pragma mark - 知识点2: UIView
- (void)createView {
    
    // 创建对象. frame属性相对于父视图坐标系.
    MyView *viewOfRed = [[MyView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    // 加到父视图上.
    [self.window addSubview:viewOfRed];
    
    // 内存管理
    [viewOfRed release];
    
    viewOfRed.backgroundColor = [UIColor redColor];
    
    // 设置tag
    viewOfRed.tag = 100;
    

    
    
    // viewOfBlack
    MyView *viewOfBlack = [[MyView alloc] initWithFrame:CGRectMake(25, 25, 50, 50)];
    [viewOfRed addSubview:viewOfBlack];
    [viewOfBlack release];
    
    viewOfBlack.backgroundColor = [UIColor blackColor];
    viewOfBlack.tag = 200;
    
    // viewOfYellow(使用自定义初始化方法, 含有color参数)
    MyView *viewOfYellow = [[MyView alloc] initWithFrame:CGRectMake(150, 150, 100, 100) backgroundColor:[UIColor yellowColor]];
    
    [self.window addSubview:viewOfYellow];
    [viewOfYellow release];
    
    viewOfYellow.tag = 300;
    
    
    // 常用API
//    viewOfRed.center = CGPointMake(300, 300);
    
    // hidden
    viewOfRed.hidden = NO;
    
    // alpha
    viewOfRed.alpha = 0.5;
    
}


#pragma mark - 知识点3: view的图层管理
- (void)managingtheViewHierarchy {
    
    // 根据tag 找到viewOfRed对象
    MyView *viewOfSearch =  [self.window viewWithTag:100];
    
    // 查看viewOfRed的父视图.
    NSLog(@"superView:%@", viewOfSearch.superview);
    
    // 查看viewOfRed的子视图
    NSLog(@"subViews:%@", viewOfSearch.subviews);
    
    // 查看window子视图
    NSLog(@"window's subViews:%@", self.window.subviews);
    
    // 把viewOfRed放在最上面
    [self.window bringSubviewToFront:viewOfSearch];
    
    
}


#pragma mark - 知识点4: UIButton(了解)
- (void)createButton {
    
    // 创建对象.
    Mybutton *buttonOfConfirm = [Mybutton buttonWithType:UIButtonTypeSystem];
    
    buttonOfConfirm.frame = CGRectMake(50, 500, 80, 40);
    [self.window addSubview:buttonOfConfirm];
    
    buttonOfConfirm.backgroundColor = [UIColor cyanColor];
    
    // 常用API
    // button上面的标题
    [buttonOfConfirm setTitle:@"确认" forState:UIControlStateNormal];
    
    // 标题颜色
    [buttonOfConfirm setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    // 添加点击事件.
    [buttonOfConfirm addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchDown]; /**< 按下去,不松手时就触发. */
    
    [buttonOfConfirm addTarget:self action:@selector(handleUP:) forControlEvents:UIControlEventTouchUpInside];
    
    buttonOfConfirm.tag = 100;
    
    
    // 取消键
    Mybutton *buttonOfCencel = [Mybutton buttonWithType:UIButtonTypeSystem];
    buttonOfCencel.frame = CGRectMake(50 + 200, 500, 80, 40);
    
    [self.window addSubview:buttonOfCencel];
    
    buttonOfCencel.backgroundColor = [UIColor orangeColor];
    
    [buttonOfCencel setTitle:@"取消" forState:UIControlStateNormal];
    
    // 添加事件
    [buttonOfCencel addTarget:self action:@selector(handleUP:) forControlEvents:UIControlEventTouchUpInside];
    
    buttonOfCencel.tag = 200;
    
}

- (void)handleUP:(UIButton *)button {

    // 根据Button的tag执行不同的操作
    switch (button.tag) {
        case 100:
            NSLog(@"点击确定按键");
            break;
            
        case 200:
            NSLog(@"点击取消按键");
            break;
            
        default:
            break;
    }
}

- (void)handleAction:(UIButton *)button {
    
    NSLog(@"确定");
    
}


#pragma mark - UIApplicationDelegate其它协议方法
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
